package main

import (
	robot "github.com/AnonymousMister/dingtalk-robot"
	"github.com/AnonymousMister/dingtalk-robot/template"
	"github.com/urfave/cli/v2"
)

var Cmd = cli.Command{
	Name:   "cmd",
	Usage:  "命令的方式 WebHook 通知工具",
	Action: runDingTalk,
	Flags:  initFlag(),
}

func initFlag() []cli.Flag {
	flag := []cli.Flag{
		&cli.StringFlag{
			Name:    "token",
			Aliases: []string{"to"},
			Usage:   "钉钉消息提示token",
			EnvVars: []string{"DINGTALK_MESSAGE_TOKEN"},
		},
		&cli.StringFlag{
			Name:    "type",
			Aliases: []string{"ty"},
			Usage:   "钉钉消息类型 默认支持 text, markdown",
			EnvVars: []string{"DINGTALK_MESSAGE_TYPE"},
		},
		&cli.StringFlag{
			Name:    "status",
			Aliases: []string{"s"},
			Usage:   "成功(success)或失败(failure) 默认 success",
			Value:   "success",
			EnvVars: []string{"DINGTALK_MESSAGE_TYPE"},
		},
		&cli.StringFlag{
			Name:    "secret",
			Aliases: []string{"se"},
			Usage:   "secret 加密",
			EnvVars: []string{"DINGTALK_MESSAGE_SECRET"},
		},
		&cli.StringFlag{
			Name:    "template.path",
			Aliases: []string{"tp"},
			Usage:   "模版 路径 加密",
			Value:   "/app/tpls",
			EnvVars: []string{"DINGTALK_MESSAGE_TEMPLATE_PATH"},
		},
		&cli.StringFlag{
			Name:    "template.name",
			Aliases: []string{"tn"},
			Usage:   "模版 名称",
			EnvVars: []string{"DINGTALK_MESSAGE_TEMPLATE_NAME"},
		},
	}
	return flag
}

func runDingTalk(c *cli.Context) error {
	y := template.NewGitlabTemplate(c.String("template.name"), c.String("template.path"))

	dingding := robot.DingDing{
		AccessToken: c.String("token"),
		Type:        c.String("type"),
		Secret:      c.String("secret"),
		Template:    y,
	}

	//dingding := dingding.DingDing{
	//	Config: &dingding.Config{
	//		AccessToken: c.String("token"),
	//		Type:        c.String("type"),
	//		Secret:      c.String("secret"),
	//	},
	//	Gitlab: &dingding.Gitlab{
	//		Status: c.String("status"),
	//	},
	//	Template: template.Template{
	//		Name: c.String("template.name"),
	//		Path: c.String("template.path"),
	//	},
	//}

	return dingding.Exec()
}
